package com.lxf.clouduser.config;

import com.lxf.cloudfeign.client.configsClient.ConfigClient;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author 罗兴发
 * @data 2025/4/11 16:10
 * @description：ES配置文件
 */
@Configuration
public class ElasticClientConfig {

    @Resource
    ConfigClient configClient;
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        // 获取配置中初始主机地址
        String nodes = configClient.getEsPath();

        // 获取es集群
        HttpHost[] httpHosts = Arrays.stream(nodes.split(",")).map(x -> {
            String[] hostInfo = x.split(":");
            return new HttpHost(hostInfo[0], Integer.parseInt(hostInfo[1]));
        }).toArray(HttpHost[]::new);

        // 构建builder
        RestClientBuilder builder = RestClient.builder(httpHosts);

        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}